package org.dce.screencapturer.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.MissingResourceException;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;

import org.dce.screencapturer.Common;
import org.dce.screencapturer.UI;

@SuppressWarnings("serial")
public abstract class ScreenCapturerAction 
	extends AbstractAction {

	private UI ui;

	public static final String ID_KEY = "id";
	public static final String CATEGORY_KEY = "category";
	
	public ScreenCapturerAction(String category, String actionId, Icon icon) {
		
		String description;
		try {
			description = Common.BUNDLE.getString(actionId);
		}catch (MissingResourceException ex){
			description = actionId;
			ex.printStackTrace();
		}
		try {
			category = Common.BUNDLE.getString(category);
		}catch (MissingResourceException ex){
			ex.printStackTrace();
		}
		putValue(ID_KEY, actionId);
		putValue(Action.NAME, description);
		putValue(Action.ACTION_COMMAND_KEY, actionId);
		putValue(Action.DEFAULT, description);
		putValue(Action.SHORT_DESCRIPTION, description);
		putValue(Action.LONG_DESCRIPTION, description);
		putValue(Action.SMALL_ICON, icon);

		putValue(CATEGORY_KEY, category);
	}
	
	public ScreenCapturerAction(String category, String text) {
		this(category,text,null);
	}

	public UI getUI() {
		return ui;
	}

	public void setUI(UI ui) {
		this.ui = ui;
	}

	public String getCategory() {
		return (String)getValue(CATEGORY_KEY);
	}

	public String getId(){
		return (String)getValue(ID_KEY);
	}
	
	public static <T extends ScreenCapturerAction> List<T> findByCategory(
			Iterable<T> actions,
			String category){
		
		List<T> selectedActions = new ArrayList<T>();
		
		for (T action:actions){
			if (category.equals(action.getCategory())){
				selectedActions.add(action);
			}
		}
		
		return selectedActions;
	}

}
